package uit.shoeshop.model;

import uit.shoeshop.dto.ProductDTO;
import uit.shoeshop.model.DetailModel;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

public class AjaxcartModel {
	
	public String addCart(int pid, int qty, HttpServletRequest request) throws SQLException{
		String html = "";
    	ProductDTO product;
    	int productId = pid;
		DetailModel detail = new DetailModel();
		product = detail.getProduct(productId);
		
    	HashMap<Integer,Integer> cart = new HashMap<Integer,Integer>();
    	Object obj = uit.shoeshop.lib.Util.getSessions("cart",request);
    	
    	if(obj==null){
    		cart.put(pid, qty);
    		if(product.getIn_stock()>=qty){
    			uit.shoeshop.lib.Util.addSessions("cart",cart,request);
    			html = "success";
    		}
    	}
    	else{
    		cart = (HashMap<Integer, Integer>) obj;
    		if(cart.get(pid)!=null){
    			if(product.getIn_stock()>=qty+cart.get(pid)){
    				cart.put(pid,qty+cart.get(pid));
    				html = "success";
        		}
    		}
    		else{
    			if(product.getIn_stock()>=qty){
    				cart.put(pid,qty);
    				html = "success";
        		}
    		}
    		if(html.equals("success"))
    			uit.shoeshop.lib.Util.addSessions("cart",cart,request);
    	}
    
    	return html;
	}
	
	public String getCart(HttpServletRequest request) throws SQLException{
		String html = "";
    	ProductDTO product;
		DetailModel detail = new DetailModel();
		
    	HashMap<Integer,Integer> cart = new HashMap<Integer,Integer>();
    	Object obj = uit.shoeshop.lib.Util.getSessions("cart",request);
    	
    	if(obj==null){
    		html = "<span class='ajax_cart_no_product' style='display: inline-block; '>(Rỗng)</span>";
    		html+= "!@#";
    		html+= "<p id='cart_block_no_products'>0 Sản phẩm</p>";
    		html+= "!@#";
    		html+= "0 VNĐ";
    	}
    	else{
    		cart = (HashMap<Integer, Integer>) obj;
    		
    		Integer total_price = 0;
    		Integer total_product = 0;
        	Set set = cart.entrySet();
        	Iterator i = set.iterator();
        	while(i.hasNext()) {
	        	Map.Entry me = (Map.Entry)i.next();
	        	Integer key = Integer.parseInt(me.getKey().toString());
	        	Integer value = Integer.parseInt(me.getValue().toString());
	        	product = detail.getProduct(key);
	        	total_price += product.getPrice()*value;
	        	total_product+= value;
        	}
        	
        	html = "<span class='ajax_cart_quantity'>"+total_product+"</span>";
    		html+= "<span class='ajax_cart_product_txt_s'> Sản Phẩm - </span>";
    		html+= "<span class='ajax_cart_total'>"+total_price+" VNĐ</span>";
        	html+= "!@#";
        	html+= "<dl class='products'>";
        	set = cart.entrySet();
        	i = set.iterator();
        	while(i.hasNext()) {
	        	Map.Entry me = (Map.Entry)i.next();
	        	Integer key = Integer.parseInt(me.getKey().toString());
	        	Integer value = Integer.parseInt(me.getValue().toString());
	        	product = detail.getProduct(key);
	        	html+= "<dt>";
	        	html+= "<span class='quantity-formated'><span class='quantity'>"+value+"</span>x</span>";
	        	html+= "<a class='cart_block_product_name' href='/shoeshop/detail.html?pid="+product.getProduct_id()+"' title='"+product.getTitle()+"'> "+product.getTitle().substring(0,8)+"...</a>";
				html+= "<span class='price'>"+(product.getPrice()*value)+" VNĐ</span>";
				html+= "</dt>";
        	}
        	html+= "</dl>";
    		html+= "!@#";
    		html+= total_price+" VNĐ";
    	}
    
    	return html;
	}
}
